/*
 * Created on Feb 26, 2005
 */
package com.scalar.chiptrack.finance.openpo.actioncommands;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntry;
import com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntryList;
import com.scalar.chiptrack.finance.openpo.ReceivingLineItem;
import com.scalar.chiptrack.finance.openpo.ReceivingLineItemList;
import com.scalar.chiptrack.finance.openpo.dao.PurchaseOrderDAO;
import com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderReceiptForm;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;

/**
 * @author Ravikanth
**/
public class PurchaseOrderReceiptUpdateCommand extends ChipTrackExtendedAction
{
	/* 
	 * Load Purchase Order Receipt
	 */
	public ActionForward executeRequest( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception
	{

		HashMap errors              = new HashMap();
		HashMap info                = new HashMap();
		String actionForward 		= "receiptSearch";
		HttpSession mSession 		= request.getSession();
		PurchaseOrderReceiptForm purchaseOrderReceiptForm = ( PurchaseOrderReceiptForm ) form;
		//PurchaseOrderForm purchaseOrder	= new PurchaseOrderForm();
		
		try
		{
			if( purchaseOrderReceiptForm.getReceipt_id() != -1 )
			{
				PurchaseOrderDAO.loadPurchaseOrderReceipt( purchaseOrderReceiptForm );
				ReceivingLineItemList receivingLineItemList = purchaseOrderReceiptForm.getReceivingLineItemList();
                int size = receivingLineItemList.size();
 
                for ( int i = 0; i < size; i++ )
                {
                    ReceivingLineItem receivingLineItem = (ReceivingLineItem)receivingLineItemList.get( i );
                    PurchaseOrderLineItemLotDataEntryList lotList = null;
                    lotList = PurchaseOrderDAO.loadLotsForReceipts( null, purchaseOrderReceiptForm.getPurchaseOrderNumber(), receivingLineItem.getProcessStepId(), purchaseOrderReceiptForm.getVendor_id(), receivingLineItem.getRecevingLineItem_Id(), purchaseOrderReceiptForm.getReceipt_id() ); //INCLUDES LOTS TO BE SELECTED ALSO    
                    receivingLineItem.setPurchaseOrderLineItemLotDataEntryList( lotList );
                }

                request.setAttribute("poTaxTypes", PurchaseOrderDAO.loadPOTaxesList() );
			}
			else
			{
				errors.put( ERROR_LOADING_RECEIVINGS_TRANSACTION_INFORMATION, null );
			}
                
			actionForward = "receipt";
		}
		catch ( Exception e )
		{
			Logger.error( ERROR_LOADING_RECEIVINGS_TRANSACTION_INFORMATION, e );
			errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_LOADING_RECEIVINGS_TRANSACTION_INFORMATION );
			mSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( ERROR_LOADING_RECEIVINGS_TRANSACTION_INFORMATION, e ) );
			request.setAttribute( "vendorsList", PurchaseOrderDAO.loadAllVendors() );
			request.setAttribute( "yearList", PurchaseOrderDAO.getYearList () );
			request.setAttribute( "productList", PurchaseOrderDAO.loadPOProductsList() );
			actionForward = "receiptSearch";
		}

		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
		return mapping.findForward( actionForward );
	}
    private PurchaseOrderLineItemLotDataEntryList getLotListForLineItem( PurchaseOrderLineItemLotDataEntryList allLotsList, int receiptLineItemId )
    {
        PurchaseOrderLineItemLotDataEntryList lotList = new PurchaseOrderLineItemLotDataEntryList();
        int size = (allLotsList != null) ? allLotsList.size() : 0;
        for ( int i=0; i < size; i++ )
        {
            PurchaseOrderLineItemLotDataEntry purchaseOrderLineItemLotDataEntry = (PurchaseOrderLineItemLotDataEntry) allLotsList.get( i );
            if ( purchaseOrderLineItemLotDataEntry.getReceiptLineItemId() == receiptLineItemId )
            {
                lotList.add ( purchaseOrderLineItemLotDataEntry );
            }
        }
        return lotList;
    }
}
